using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using UniversalAdmin.Application.Commands;
using UniversalAdmin.Application.Dto;
using UniversalAdmin.Application.Services;
using System.Text.Json;
using UniversalAdmin.Shared.Enums;
using UniversalAdmin.Api.Filters;

[ApiController]
[Route("api/system/configs")]
[Authorize]
public class SystemConfigsController : ControllerBase
{
    private readonly IAppSystemConfigService _systemConfigService;
    public SystemConfigsController(IAppSystemConfigService systemConfigService) { _systemConfigService = systemConfigService; }

    [HttpGet]
    [RequirePermission("system.view")] // 需要查看系统配置权限
    public async Task<ApiResult> Get([FromQuery] int? page = null, [FromQuery] int? size = null, [FromQuery] string? search = null)
    {
        if (page.HasValue && size.HasValue)
        {
            var query = new PagedQuery { Page = page.Value, Size = size.Value, Search = search };
            var result = await _systemConfigService.GetPagedAsync(query);
            return new ApiResult { Code = result.Code, Message = result.Message, Data = result.Data, Timestamp = result.Timestamp };
        }
        else
        {
            var result = await _systemConfigService.GetAllAsync(search);
            return new ApiResult { Code = result.Code, Message = result.Message, Data = result.Data, Timestamp = result.Timestamp };
        }
    }

    [HttpGet("{id}")]
    [RequirePermission("system.view")] // 需要查看系统配置权限
    public async Task<ApiResult<SystemConfigDto>> GetById(Guid id)
        => await _systemConfigService.GetByIdAsync(id);

    [HttpPut("{id}")]
    [RequirePermission("system.edit")] // 需要编辑系统配置权限
    public async Task<ApiResult> Update(Guid id, [FromBody] UpdateSystemConfigDto dto)
        => await _systemConfigService.UpdateAsync(id, dto);

    [HttpPost("")]
    [RequirePermission("system.create")] // 需要创建系统配置权限
    public async Task<ApiResult> Create([FromBody] CreateSystemConfigDto dto)
        => await _systemConfigService.CreateAsync(dto);

    [HttpDelete("{id}")]
    [RequirePermission("system.delete")] // 需要删除系统配置权限
    public async Task<ApiResult> Delete(Guid id)
        => await _systemConfigService.DeleteAsync(id);
} 